Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		state: "add",
		boxTop: "",
		boxBottom: ""
	},
	onLoad: function(options) {
		let that = this;
		//设置滚动区域高度
		wx.getSystemInfo({
			success(res) {
				that.setData({
					winHeight: (750 / res.windowWidth * res.windowHeight - 178) + "rpx"
				});
			}

		});
		this.setData({
			type: options.type,
			addressId: options.addressId ? options.addressId : ""
		})
		console.log("")
		console.log(options.addressId);
	},
	onShow() {
		this.getAddressData();
	},
	/*获取地址列表数据*/
	getAddressData() {
		let that = this;
		//请求的参数   
		let parameter = {
			url: getApp().globalData.baseUrl + '/applet/address/getMemberAddress.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode
			}
		}
		//请求
		//console.log(that.data.addressId+"------------------");
		getApp().getRequest(parameter).then((res) => {
			if (res.data.obj.length > 0) {
				let addressAry = [];
				res.data.obj.forEach(element => {
					let addressObj = {};
					//有被选中的地址
					if (that.data.addressId) {
						    addressObj.addressId = element.addressId;
							addressObj.addressFixed = element.addressFixed;
							addressObj.addressDetail = element.addressDetail;
							addressObj.isDefault = element.isDefault;
							addressObj.nickName = element.nickName;
							addressObj.mobile = element.mobile;
						if (element.addressId == that.data.addressId) {
							addressObj.select = true;
						} else {
							addressObj.select = false;
							
						}
					} else {
						 
						//没有被选中地址时，用默认的
						if (element.isDefault == 1) {
							addressObj.select = true;
						} else {
							addressObj.select = false;
						}
						    addressObj.addressId = element.addressId;
							addressObj.addressFixed = element.addressFixed;
							addressObj.addressDetail = element.addressDetail;
							addressObj.isDefault = element.isDefault;
							addressObj.nickName = element.nickName;
							addressObj.mobile = element.mobile;
					}
					addressAry.push(addressObj);
				});
				//console.log(addressAry);
				that.setData({
					addressData: addressAry,
					boxTop: "",
					boxBottom: "0rpx",
					boxWidth: "702rpx",
					isEmpty: true
				});
			} else {
				that.setData({
					boxTop: "728rpx",
					boxBottom: "",
					boxWidth: "360rpx",
					isEmpty: false
				});
			}
		}).catch((err) => {
			that.setData({
				boxTop: "728rpx",
				boxBottom: "",
				boxWidth: "360rpx",
				isEmpty: false
			});
		});

	},
	/*编辑地址*/
	editAddress(e) {
		let aId = e.currentTarget.dataset.id;
		let that = this;
		wx.navigateTo({
			url: "../reserveAddress/reserveAddress?type=2&state=edit&aId=" + aId
		});
	},
	//新增地址
	addressAdd() {},
	bindTouchStart(e) {
		this.startTime = e.timeStamp;
	},
	bindTouchEnd(e) {
		this.endTime = e.timeStamp;
	},
	//点击选择地址 
	bindTap(e) {
		if (this.endTime - this.startTime < 350) {
			//固定地址/详细地址放入全局
			getApp().globalData.common.reservebAdd = e.currentTarget.dataset.addressfixed;
			getApp().globalData.common.reservesAdd = e.currentTarget.dataset.addressdetail;
			getApp().globalData.common.addressId = e.currentTarget.dataset.id;

			// console.log(e.currentTarget.dataset.addressfixed+"--------------拼团");
			let pages = getCurrentPages(); //当前页面
			let prevPage = pages[pages.length - 2]; //上一页面
			prevPage.setData({ //直接给上移页面赋值
				aId: e.currentTarget.dataset.id
			});
			wx.navigateBack({ //返回
				delta: 1
			})
		}
	},
	//长按 删除一条地址
	bingLongTap(e) {
		let aId = e.currentTarget.dataset.id;
		let that = this;
		//请求的参数   
		let parameter = {
			url: getApp().globalData.baseUrl + '/applet/address/deleteDeliveryAddress.do',
			data: {
				addressId: aId
			}
		}
		wx.showModal({
			content: "确定删除地址吗？",
			success(res) {
				if (res.confirm) {
					//请求
					getApp().getRequest(parameter).then((res) => {
						if (res.data.obj) {
							getApp().globalData.common.reservebAdd = "";
							getApp().globalData.common.reservesAdd = "";
							getApp().globalData.common.addressId = "";
							that.getAddressData();
						}
					}).catch((err) => {
						console.log(err);
					});
				}
			}
		});
	}
});